import { obServer } from "./observe/index";

export function initState(vm) {
  let ops = vm.$options;
  if (ops.props) {
    initProps();
  }
  if (ops.data) {
    initData(vm);
  }
  if (ops.watch) {
    initWatch();
  }
  if (ops.computed) {
    initComputed();
  }
  if (ops.methods) {
    initMethods();
  }
}

// 对data初始化1
function initData(vm) {
  let data = vm.$options.data;
  data = vm._data = typeof data === "function" ? data.call(vm) : data;
  //将data上的所有属性代理到实例vm上
  for (let key in data) {
    proxy(vm, "_data", key);
  }
  obServer(data);
}

function proxy(vm, source, key) {
  Object.defineProperty(vm, key, {
    get() {
      return vm[source][key];
    },
    set(newValue) {
      vm[source][key] = newValue;
    },
  });
}

function initProps() {}
function initWatch() {}
function initComputed() {}
function initMethods() {}
